/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.manage;

import controller.MySQL;
import controller.query.QueryController;
import controller.update.UpdateController;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Felipe
 */
public abstract class ViewBaseUpdate extends javax.swing.JDialog {

    /**
     * Creates new form AlterarCliente
     */
        
    private QueryController mTableQueryController;    
    protected UpdateController mUpdateController;    
    private String mQuery;
    
    protected ViewBaseUpdate(String pQuery) {
        setModal(true);
        mQuery = pQuery;
        mTableQueryController = new QueryController(new QueryController.QueryListener() {
            @Override
            public void onQueryComplete(List pList) {    
                ViewBaseUpdate.this.onTableQueryComplete(pList);
            }

            @Override
            public void onQueryError(Exception e) {
                MySQL.handleException(e);
            }
        });
        mUpdateController = new UpdateController(new UpdateController.UpdateListener() {
            @Override
            public void onUpdateComplete() {
                JOptionPane.showMessageDialog(ViewBaseUpdate.this, "Registro atualizado com sucesso.");
                select();
            }

            @Override
            public void onUpdateError(String message, Exception e) {
                if (e == null) {
                    JOptionPane.showMessageDialog(ViewBaseUpdate.this, message);
                } else {
                    MySQL.handleException(e);
                }
            }
        });
    } 
    
    protected abstract void onTableQueryComplete(List pList);    
    
    protected abstract void clearFields(boolean clearFilter);
    
    protected void select() {
        mTableQueryController.select(mQuery);         
    }
    
    protected void select(String pQuery) {
        mTableQueryController.select(pQuery);        
    };       
    
    @Override
    public void setVisible(boolean flag) {
        if (flag && !isVisible()) {
            clearFields(true);
            select();
        }
        super.setVisible(flag);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 395, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
